package com.tony.boot.control;

import com.tony.boot.entity.Book;
import com.tony.boot.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/book")
public class BookControl {
    @Autowired
    BookRepository bookRepository;
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public @ResponseBody Book addBook(@RequestBody Book pBook){
        return bookRepository.save(pBook);
    }
    @RequestMapping(path = "/addAll", method = RequestMethod.POST)
    public @ResponseBody List<Book> addBooks(@RequestBody List<Book> pBooks){
        return bookRepository.saveAll(pBooks);
    }
    @RequestMapping(path = "/delete", method = RequestMethod.DELETE)
    public String delete(){
        bookRepository.deleteAll();
        return "delete";
    }
    @RequestMapping(path = "/deleteById/{id}", method = RequestMethod.DELETE)
    @CacheEvict(value = "book", key = "#id")
    public String deleteById(@PathVariable(name = "id") Integer id){
        bookRepository.deleteById(id);
        return  "deleted id = " + id;
    }

    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public @ResponseBody Object update(@RequestBody Book pBook){
        Book Book = bookRepository.findById(pBook.getId()).orElse(null);
        if (Book == null){
            return "no this Book, name : " + pBook.getName();
        } else {
            return bookRepository.save(pBook);
        }
    }

    @RequestMapping(path = "/find",method = RequestMethod.GET)
    public  @ResponseBody
    List<Book> find(){
        return bookRepository.findAll();
    }

    @RequestMapping(path = "/findById/{id}",method = RequestMethod.GET)
    @Cacheable(value = "book", key = "#id", condition = "#id != 123"
            , keyGenerator = "", cacheResolver = "", cacheManager = ""
            , unless = "")
    //@CachePut(value = "user", key = "#id", condition = "#id != '321'")
//    @Caching(cacheable = @Cacheable(value = "book", key = "#id", condition = "#id != 123")
//            , put = @CachePut(value = "book", key = "#id", condition = "#id != 321")
//            , evict = @CacheEvict(value = "book", key = "#id", condition = "#id == 123")
//    )
    public  @ResponseBody Book findById(@PathVariable(name = "id") Integer id){
        return bookRepository.findById(id).get();
    }
}
